<?php


namespace app\admin\controller;

use think\Controller;
use think\Db;
use think\Exception;

class Slide extends Base
{

    public function index()
    {

        if (request()->isAjax()) {

            $res = Db::name('slide')->order('id', 'desc')->select();
            $this->success('ok', '', $res);

        } else {
            return $this->fetch();
        }

        // for ($i=0;$i<7;$i++){
        //     foreach ($res as $index => $re) {
        //         $data=$re;
        //         unset($data['id']);
        //         // var_dump($data);
        //         $gd->insertGetId($data);
        //     }
        // }

    }


    public function add()
    {
        if (request()->isAjax()) {

            $title = input('title');
            if (!$title) {
                $this->error('error');
            }
            $sl = Db::name('slide');
            if ($sl->where('title', $title)->find()) {
                $this->error('标题重复了');
            }
            $sl->insertGetId(['title' => $title]);
            $this->success('操作成功');

        } else {
            return $this->fetch();
        }
    }


    public function detail()
    {

        if (request()->isAjax()) {
            $id = input('id');
            $res = Db::name('slide_content')->where('slide_id', $id)->select();

            $qiniu_config = cmf_get_option('qiniu');
            // 七牛样式处理
            $q_thumb = "?imageMogr2/auto-orient/thumbnail/!60x120r/gravity/Center/quality/100/interlace/0";
            foreach ($res as $index => $re) {
                $res[$index]['src'] = $qiniu_config['domain'] . '/' . $re['src'] . $q_thumb;
            }

            $this->success('', '', $res);
        } else {
            $id = input('id');
            $this->assign('id', $id);
            Db::name('slide')->find($id);
            return $this->fetch();
        }


    }


    public function pic_add()
    {

        $data = input('');
        unset($data['file']);
        Db::name('slide_content')->insertGetId($data);
        $this->success('操作成功');
        // try {
        //     Db::name('slide_content')->insertGetId($data);
        //     $this->success();
        // } catch (Exception $exception) {
        //     $this->error();
        // }

    }


    public function pic_del()
    {
        $id = input('id');
        Db::name('slide_content')->where('id', $id)->delete();
        $this->success('操作成功');
    }


    public function del()
    {
        $id = input('id');
        Db::name('slide')->where('id', $id)->delete();
        Db::name('slide_content')->where('slide_id', $id)->delete();

        $this->success('操作成功');
    }
}
